import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../generated/l10n.dart';
import '../../res/colors.dart';
import '../../res/text_style.dart';
import 'over_scroll_behavior.dart';

typedef FacilitieCallback = void Function(int index);

class DialogFacilities extends StatelessWidget {
  FacilitieCallback? facilitieCallback;
  var facilitieIndex = 0.obs;

  DialogFacilities({
    super.key,
    this.facilitieCallback,
  });

  @override
  Widget build(BuildContext context) {
    return Material(

        ///透明样式
        type: MaterialType.transparency,

        ///dialog居中
        child: Center(

            ///取消ListView滑动阴影
            child: ScrollConfiguration(
                behavior: OverScrollBehavior(),

                ///ListView 的shrinkWrap属性可适应高度（有多少占多少）
                child: ListView(
                  shrinkWrap: true,
                  children: [
                    ///背景及内容、边距、圆角等，必须包裹在ListView中
                    Container(
                      width: double.infinity,
                      margin: EdgeInsets.only(right: 30.w, left: 30.w),
                      child: Container(
                        decoration: const ShapeDecoration(
                          color: Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                              Radius.circular(20.0),
                            ),
                          ),
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            ///标题、内容

                            Text(
                              S.current.facilitie,
                              textAlign: TextAlign.center,
                              style: TextStyles.textStyle(
                                  ColorStyle.black, 16.sp,
                                  fontWeight: FontWeight.w600),
                            ).marginOnly(top: 20.h),
                            Padding(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 10.w, vertical: 17.h),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Obx(() => _facilitieButton(
                                          () => facilitieIndex.value = 0,
                                          S.current.unlimited,
                                          facilitieIndex.value == 0))
                                      .marginOnly(right: 10.h),
                                  Obx(() => _facilitieButton(
                                          () => facilitieIndex.value = 1,
                                          S.current.IntScreen,
                                          facilitieIndex.value == 1))
                                      .marginOnly(right: 10.h),
                                  Obx(() => _facilitieButton(
                                      () => facilitieIndex.value = 2,
                                      S.current.Whiteboard,
                                      facilitieIndex.value == 2)),
                                ],
                              ),
                            ),

                            ///确定、取消按钮
                            Flex(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              direction: Axis.horizontal,
                              children: [
                                ///对半分
                                _textButton(
                                        () {},
                                        S.current.cancel,
                                        const Color(0xFF333333),
                                        const Color(0xffeeeeee))
                                    .marginOnly(right: 20.w),

                                _textButton(() {
                                  facilitieCallback!(facilitieIndex.value);
                                }, S.current.confirm, Colors.white,
                                    const Color(0xff1877ff)),
                              ],
                            ).marginOnly(bottom: 20.h)
                          ],
                        ),
                      ),
                    ),
                  ],
                ))));
  }

  Widget _textButton(
      VoidCallback? tap, String text, Color textColor, Color btuColor) {
    return GestureDetector(
      onTap: () {
        Get.back();
        if (tap != null) {
          tap();
        }
      },
      child: Container(
        alignment: Alignment.center,
        padding:
            EdgeInsets.only(left: 16.w, right: 16.w, top: 6.h, bottom: 6.h),
        decoration: BoxDecoration(
          color: btuColor,
          borderRadius: BorderRadius.circular(4.r),
        ),
        child: Text(
          text,
          style: TextStyle(
              color: textColor, fontWeight: FontWeight.w500, fontSize: 14.sp),
        ),
      ),
    );
  }

  Widget _facilitieButton(VoidCallback? tap, String text, bool isSelect) {
    return GestureDetector(
      onTap: () {
        if (tap != null) {
          tap();
        }
      },
      child: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.only(left: 6.w, right: 6.w, top: 6.h, bottom: 6.h),
        decoration: BoxDecoration(
          color:
              isSelect ? ColorStyle.color_ff1877ff : ColorStyle.color_ffeeeeee,
          borderRadius: BorderRadius.circular(15.r),
        ),
        child: FittedBox(
          fit: BoxFit.fill,
          child: Text(
            text,
            style: TextStyle(
                color: isSelect ? Colors.white : ColorStyle.color333333,
                fontWeight: FontWeight.w500,
                fontSize: 14.sp),
          ),
        ),
      ),
    );
  }
}
